package com.baosight.risk.web.fltd;

import com.baosight.risk.api.fltd.FlaaNodeAuthoService;
import com.baosight.risk.api.fltd.FltdProcessService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.service.fltd.impl.AuthoTypeTemplate;
import com.baosight.risk.service.fltd.vo.FlaaNodeAuthoTypeVO;
import com.baosight.risk.service.fltd.vo.FlaaNodeAuthoVO;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;
import com.baosight.risk.service.fltd.vo.NodeTransitionVO;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者： lishaoqing
 * 时间： 2019年07月17日
 * 描述： FltdTaskController
 * 版本: v1.0
 */
@Api(value = "流程节点授权", description = "流程节点授权", produces = MediaType.APPLICATION_JSON)
@Slf4j
@RestController
@RequestMapping("/fltd/node")
public class FlaaNodeAuthoController extends BaseController {

    @Autowired
    private FlaaNodeAuthoService flaaNodeAuthoService;

    @Autowired
    private FltdProcessService fltdProcessService;

    @Autowired
    private AuthoTypeTemplate authoTypeService;

    @ApiOperation("新增流程授权规则")
    @PostMapping("save")
    public AjaxResult save(@RequestBody List<FlaaNodeAuthoVO> auths) throws Exception {
        flaaNodeAuthoService.save(auths, getUser().getUserCode());
        return AjaxResult.success();
    }

    @ApiOperation("更新流程授权规则")
    @PutMapping("update")
    public AjaxResult update(@RequestBody List<FlaaNodeAuthoVO> nodes) throws Exception {
        flaaNodeAuthoService.update(nodes, getUser().getUserCode());
        return AjaxResult.success();
    }

    @ApiOperation("查询流程授权规则")
    @GetMapping("nodes")
    public AjaxResult nodes(String flowNo) {
        return AjaxResult.success("查询成功", flaaNodeAuthoService.selectNodesByFlowNo(flowNo));
    }

    @ApiOperation("查询审核按钮")
    @GetMapping("button")
    public AjaxResult authButton(FltdTaskVO taskVO) {
        Map<String, Object> resultMap = flaaNodeAuthoService.getTransitionButton(taskVO);
        if (!"0".equals(resultMap.get("code"))) {
            return AjaxResult.error((String) resultMap.get("msg"));
        }
        List<NodeTransitionVO> transitions = (List<NodeTransitionVO>) resultMap.get("transitions");
        return AjaxResult.success("查询成功", transitions);
    }

    @ApiOperation("获取下一节点审批人")
    @GetMapping("user")
    public AjaxResult authUser(FltdTaskVO taskVO) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }
        Map<String, Object> resultMap = flaaNodeAuthoService.getNextUser(taskVO, user);
        if (!"0".equals(resultMap.get("code"))) {
            return AjaxResult.error((String) resultMap.get("msg"));
        }
        return AjaxResult.success("查询成功", resultMap);
    }

    @ApiOperation("获取下一节点审批人")
    @GetMapping("userBranch")
    public AjaxResult authUserBranch(FltdTaskVO taskVO) throws Exception {
        XtzyUser user = getUser();
        if (user == null || user.getId() == null) {
            return AjaxResult.error("请先登录！");
        }
        Map<String, Object> resultMap = flaaNodeAuthoService.getNextUserBranch(taskVO, user);
        if (!"0".equals(resultMap.get("code"))) {
            return AjaxResult.error((String) resultMap.get("msg"));
        }
        return AjaxResult.success("查询成功", resultMap);
    }

    @ApiOperation("测试获取审批人接口")
    @PostMapping("testUser")
    public AjaxResult testUser(FlaaNodeAuthoTypeVO authoTypeVO, @RequestBody FltdTaskVO currentTask) throws Exception {

        //流程参与者
        List<XtzyUser> userList = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        param.put("authInfo", authoTypeVO);
        param.put("currentUser", fltdProcessService.getStarter());
        param.put("currentTask", currentTask);
        param.put("userList", userList);
        authoTypeService.parse(authoTypeVO.getAuthoRange(), param);
        return AjaxResult.success("查询成功", userList);
    }

    @ApiOperation("更新流程授权规则")
    @PostMapping("clean")
    public AjaxResult cleanNodes(@RequestBody List<FlaaNodeAuthoVO> nodes) throws Exception {
        flaaNodeAuthoService.cleanNodes(nodes);
        return success();
    }

}
